package com.woniu.sharepark.util;

import java.util.Random;

/**
 * @ClassName: InvitationUtil
 * @Description: 此工具为邀请码随机生成工具
 * @Author: Zongjie.Li
 * @Date: 2019/6/10
 * @Version: 1.0
 **/
public class InvitationUtil {
    /**
     * 此方法用于生成任意位数的邀请码，邀请码由数字和小写字母组成
     *
     * @param codeBound 邀请码位数
     * @return java.lang.String
     * @Author Zongjie.Li
     * @Date 2019/6/10
     * @Version 1.1
     *
     * 增加了参数，使方法可以接受任意位数，并产生随机码
     * 优化代码结构，更改了(int)Math.Random()*XX的代码为 new Random().nextInt()
     */
    public static String randomGeneration(Integer codeBound) {
        String code = "";
        for (int i = 0; i < codeBound; i++) {
            int asciiRandomNumber = new Random().nextInt(36);
            char asciiCode;
            if (asciiRandomNumber < 10) {
                asciiCode = (char) (asciiRandomNumber + 48);
            } else {
                asciiCode = (char) (asciiRandomNumber + 87);
            }
            code = code + asciiCode;
        }
        return code;
    }
}
